
#import "TorrentPeerListViewController.h"
#import "TorrentPeerDetailsViewController.h"

@implementation TorrentPeerListViewController

//
// torrent info
//
@synthesize torrentInfo														= _torrentInfo;

//
// seeder list
//
@synthesize seederList														= _seederList;

//
// leecher list
//
@synthesize leecherList														= _leecherList;

//
// init
//
- (id)init
{
	//
	// call super
	//
	self																	= [super initWithStyle:UITableViewStyleGrouped];
	if(!self)
		return nil;

	//
	// setup title
	//
	self.title																= NSLocalizedStringFromTable(@"Peer List", @"TorrentPeerListViewController", nil);
	
	//
	// setup refresh button
	//
	self.navigationItem.rightBarButtonItem									= [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(refreshPeerList:)] autorelease];
	
	return self;
}

//
// dealloc
//
- (void)dealloc
{
	self.torrentInfo														= nil;
	self.seederList															= nil;
	self.leecherList														= nil;
	
	[super dealloc];
}

//
// refresh peer list
//
- (void)refreshPeerList:(id)sender
{
	//
	// get list
	//
	NexusClient* client														= [NexusClient sharedClient];
	id getTorrentPeerListFinished = ^void(GetTorrentPeerListRequest* httpRequest)
	{
		[ActivityView removeView];
		if(!httpRequest.error && httpRequest.response.statusCode == 200)
		{
			self.seederList													= httpRequest.seederList;
			self.leecherList												= httpRequest.leecherList;
			self.torrentInfo.seederCount									= self.seederList.count;
			self.torrentInfo.leecherCount									= self.leecherList.count;
			[self.tableView reloadData];
		}
		else
		{
			NSString* message												= [httpRequest errorDescription];
			NSString* title													= NSLocalizedStringFromTable(@"Get Peer List Failed", @"TorrentPeerListViewController", nil);
			[UIAlertView alertViewWithTitle:title message:message cancel:NSLocalizedString(@"OK", nil)];
		}
	};
	[ActivityView showActivityView:NSLocalizedStringFromTable(@"Getting Peer List", @"TorrentPeerListViewController", nil) networking:YES];
	[client getTorrentPeerList:self.torrentInfo.torrentId callback:getTorrentPeerListFinished];
}

//
// did load
//
- (void)viewDidLoad
{	
	[super viewDidLoad];
	[self refreshPeerList:nil];
}

//
// get section count
//
- (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView
{
	return 2;
}

//
// get title
//
- (NSString*)tableView:(UITableView*)tableView titleForHeaderInSection:(NSInteger)section
{
	if(section == 0)
		return self.seederList ? [NSString stringWithFormat:NSLocalizedStringFromTable(@"%d Seeders", @"TorrentPeerListViewController", nil), self.seederList.count] : nil;
	
	return self.leecherList ? [NSString stringWithFormat:NSLocalizedStringFromTable(@"%d Leechers", @"TorrentPeerListViewController", nil), self.leecherList.count] : nil;
}

//
// get footer
//
- (NSString*)tableView:(UITableView*)tableView titleForFooterInSection:(NSInteger)section
{
	return section == 1 ? self.torrentInfo.title : nil;
}

//
// get row count
//
- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{
	NSInteger count															= section == 0 ? self.seederList.count : self.leecherList.count;
	return count || !(section == 0 ? self.seederList : self.leecherList) ? count : 1;
}

//
// get cell
//
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
	UITableViewCell* theCell												= nil;
	if(indexPath.row < (indexPath.section == 0 ? self.seederList.count : self.leecherList.count))
	{
		theCell																= [tableView dequeueReusableCellWithIdentifier:@"s>"];
		if(!theCell)
			theCell															= [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"s>"] autorelease];
		
		TorrentPeerInfo* peerInfo											= indexPath.section == 0 ? [self.seederList objectAtIndex:indexPath.row] : [self.leecherList objectAtIndex:indexPath.row];
		theCell.textLabel.text												= peerInfo.userName;
		if(indexPath.section == 0)
			theCell.detailTextLabel.text									= [NSString stringWithFormat:@"%@ [%@]@%@", peerInfo.ipType, peerInfo.uploadedSize, peerInfo.shareRatio];
		else
			theCell.detailTextLabel.text									=[NSString stringWithFormat:@"%@ %@ %@", peerInfo.ipType, peerInfo.completed, peerInfo.shareRatio];
		
		theCell.accessoryType												= UITableViewCellAccessoryDisclosureIndicator;
	}
	else
	{
		theCell																= [tableView dequeueReusableCellWithIdentifier:@"d"];
		if(!theCell)
			theCell															= [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"d"] autorelease];
		
		theCell.textLabel.text												= NSLocalizedStringFromTable(@"None", @"TorrentPeerListViewController", nil);
		theCell.accessoryType												= UITableViewCellAccessoryNone;
		theCell.selectionStyle												= UITableViewCellSelectionStyleNone;
	}

	return theCell;
}

//
// select row
//
- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
	if(indexPath.row < (indexPath.section == 0 ? self.seederList.count : self.leecherList.count))
	{
		TorrentPeerDetailsViewController* torrentPeerDetailsViewController	= [[[TorrentPeerDetailsViewController alloc] init] autorelease];
		torrentPeerDetailsViewController.torrentInfo						= self.torrentInfo;
		torrentPeerDetailsViewController.peerInfo							= indexPath.section == 0 ? [self.seederList objectAtIndex:indexPath.row] : [self.leecherList objectAtIndex:indexPath.row];
		[self.navigationController pushViewController:torrentPeerDetailsViewController animated:YES];
	}
}

@end
